package com.youku.plugins.topbarV4.view
{
   import flash.display.Sprite;
   import flash.text.TextField;
   import com.youku.baseUI.BasePlayerButton;
   import flash.utils.Timer;
   import com.youku.utils.AssetUtil;
   import flash.text.TextFormat;
   import com.youku.skin.YaheiFontManager;
   import flash.events.TimerEvent;
   import com.youku.plugins.topbarV4.TopbarEvent;
   import flash.events.MouseEvent;
   import com.youku.utils.ReportUtil;
   
   public class Topbar extends Sprite
   {
      
      public function Topbar() {
         super();
         this.initItems();
         this.attchEvents();
      }
      
      protected var _background:Sprite;
      
      protected var _titleField:TextField;
      
      protected var _sizeDefer:SizeDefer;
      
      protected var _timeField:TextField;
      
      protected var _closeButton:BasePlayerButton;
      
      protected var _button:BasePlayerButton;
      
      protected var _refreshTimeTimer:Timer;
      
      protected var _refreshTimeOut:Number = 5000.0;
      
      protected var _width:Number;
      
      protected var _height:Number;
      
      protected var _titleString:String = "";
      
      private function initItems() : void {
         this._background = AssetUtil.getAsset("V4_panel_bg") as Sprite;
         this._titleField = AssetUtil.getTextField(false);
         this._titleField.width = 250;
         this._titleField.height = 25;
         this._titleField.defaultTextFormat = new TextFormat(YaheiFontManager.MICROSOFT_YAHEI,16,10987431);
         this._sizeDefer = new SizeDefer();
         this._timeField = AssetUtil.getTextField(false);
         this._timeField.width = 100;
         this._timeField.height = 25;
         this._timeField.defaultTextFormat = new TextFormat(YaheiFontManager.MICROSOFT_YAHEI,18,10987431);
         this._closeButton = AssetUtil.getStyleButton("C_NonfullScreen");
         AssetUtil.restoreButtonSize(this._closeButton);
         addChild(this._background);
         addChild(this._titleField);
         addChild(this._sizeDefer);
         addChild(this._timeField);
         addChild(this._closeButton);
         this._refreshTimeTimer = new Timer(this._refreshTimeOut);
         this._refreshTimeTimer.addEventListener(TimerEvent.TIMER,this.onRefreshTime);
         this._refreshTimeTimer.start();
         this.onRefreshTime(null);
      }
      
      private function attchEvents() : void {
         this._sizeDefer.addEventListener(TopbarEvent.TOPBAR_50_CLICK,this.onEvent);
         this._sizeDefer.addEventListener(TopbarEvent.TOPBAR_75_CLICK,this.onEvent);
         this._sizeDefer.addEventListener(TopbarEvent.TOPBAR_100_CLICK,this.onEvent);
         this._sizeDefer.addEventListener(TopbarEvent.TOPBAR_FULL_CLICK,this.onEvent);
         this._closeButton.addEventListener(MouseEvent.CLICK,this.onCloseButtonClick);
      }
      
      private function onCloseButtonClick(param1:MouseEvent) : void {
         ReportUtil.sendUserActionLog(ReportUtil.QUIT_FULLSCREEN,ReportUtil.METHOD_CLOSE_CLICK_LEFT_TOP);
         dispatchEvent(new TopbarEvent(TopbarEvent.TOPBAR_CLOSE_CLICK));
      }
      
      private function onEvent(param1:TopbarEvent) : void {
         dispatchEvent(new TopbarEvent(param1.type,param1.data));
      }
      
      private function onRefreshTime(param1:TimerEvent) : void {
         var _loc2_:Date = new Date();
         var _loc3_:* = "";
         if(_loc2_.getHours() < 12)
         {
            _loc3_ = "上午";
         }
         else
         {
            _loc3_ = "下午";
         }
         var _loc4_:String = (_loc2_.getHours() < 10?"0":"") + _loc2_.getHours();
         var _loc5_:String = (_loc2_.getMinutes() < 10?"0":"") + _loc2_.getMinutes();
         this._timeField.text = _loc4_ + ":" + _loc5_;
      }
      
      public function setSize(param1:Number, param2:Number) : void {
         this._width = param1;
         this._height = param2;
         this._background.width = this._width;
         this._background.height = 34;
         this._titleField.width = (this._background.width - this._sizeDefer.width) / 2 - 150;
         this.updateLayout();
         this.title = this._titleString;
      }
      
      private function updateLayout() : void {
         this._background.x = this._background.y = 0;
         this._titleField.x = 5;
         this._titleField.y = 4;
         this._sizeDefer.x = (this._background.width - this._sizeDefer.width) / 2;
         this._sizeDefer.y = 0;
         this._closeButton.x = this._background.width - this._closeButton.width - 8;
         this._closeButton.y = -3;
         this._timeField.x = this._background.width - 120;
         this._timeField.y = 2;
      }
      
      public function set title(param1:String) : void {
         this._titleString = param1;
         this._titleField.text = param1;
         var _loc2_:String = param1;
         while(this._titleField.textWidth > this._titleField.width && _loc2_.length > 0)
         {
            _loc2_ = _loc2_.substr(0,_loc2_.length - 1);
            this._titleField.text = _loc2_ + "...";
         }
      }
      
      public function setScreenMode(param1:String) : void {
         this._sizeDefer.selectButton(param1);
      }
      
      public function upgradeMode() : void {
         this._sizeDefer.upgradeMode();
      }
      
      public function downgradeMode() : void {
         this._sizeDefer.downgradeMode();
      }
      
      override public function get height() : Number {
         return this._background.height;
      }
      
      public function set enableSizeDefer(param1:Boolean) : void {
         this._sizeDefer.enable = param1;
      }
   }
}
